package com.huika.dealontop.control.shops.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huika.dealontop.R;
import com.huika.dealontop.control.base.BaseAda;
import com.huika.dealontop.control.nearby.activity.MerchantDetailActivity;
import com.huika.dealontop.support.domain.SubbranchBean;
import com.huika.dealontop.utils.DistanceUtils;
import com.huika.dealontop.views.MMAlert;
import com.huika.dealontop.views.PromptOperatDialog;

/**
 * auther: chenyuan.
 * time: 2016/1/12 09:14
 * description:通用分店适配器
 */
public class SubbranchAdapter extends BaseAda<SubbranchBean> {

    public SubbranchAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_common_subbranch, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        final SubbranchBean bean = getItem(position);
        if (!TextUtils.isEmpty(bean.ShopName)){
            vh.merchant_name_tv.setText(bean.ShopName);
        }

        vh.merchant_address_tv.setText(bean.ShopAddress);
        if (!TextUtils.isEmpty(bean.Kilometer)) {
            vh.diatance_tv.setText(DistanceUtils.formatMeter(Double.parseDouble(bean.Kilometer)));
        } else {
            vh.diatance_tv.setText(R.string.common_unknown);
        }
        vh.tel_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!TextUtils.isEmpty(bean.Tel)&&!TextUtils.isEmpty(bean.Phone)){
                    MMAlert.createTwoChoicAlertNoTitle(mContext, bean.Tel, bean.Phone, new MMAlert.DialogOnItemClickListener() {
                        @Override
                        public void onItemClickListener(View v, int position) {
                            switch (position) {
                                case 0:// tel
                                    startCallPhone(bean.Tel);
                                    break;
                                case 1:// phone
                                    startCallPhone(bean.Phone);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }).show();
                }else if(!TextUtils.isEmpty(bean.Tel)&&TextUtils.isEmpty(bean.Phone)){
                    confirmDialog(bean.Tel);
                }else if(TextUtils.isEmpty(bean.Tel)&&!TextUtils.isEmpty(bean.Phone)){
                    confirmDialog(bean.Phone);
                }




            }
        });

        vh.describe_loy.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
               Intent intent= new Intent(mContext, MerchantDetailActivity.class);
                intent.putExtra(MerchantDetailActivity.Merchant_ID, String.valueOf(bean.MerchantID));
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }

    private void startCallPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
        mContext.startActivity(intent);
    }


    class ViewHolder {

        public TextView merchant_name_tv;//商家名称
        public TextView merchant_address_tv;//商家地址
        public TextView diatance_tv;//商家距离
        public ImageView tel_iv;//商家电话
        public LinearLayout describe_loy;//左边

        public ViewHolder(View convertView) {
            merchant_name_tv = (TextView) convertView.findViewById(R.id.merchant_name_tv);
            diatance_tv = (TextView) convertView.findViewById(R.id.diatance_tv);
            merchant_address_tv = (TextView) convertView.findViewById(R.id.merchant_address_tv);
            tel_iv = (ImageView) convertView.findViewById(R.id.tel_iv);
            describe_loy = (LinearLayout) convertView.findViewById(R.id.describe_loy);
        }

    }

    private void confirmDialog(final String tel) {
        View view=View.inflate(mContext, R.layout.submit_confirm_dialog, null);
        TextView des_tv= (TextView) view.findViewById(R.id.des_tv);
        des_tv.setText(tel);
        final PromptOperatDialog dialog_confirm = new PromptOperatDialog(mContext, view);
        dialog_confirm.setTitleGone();
        dialog_confirm.getConfirmBtn().setText(mContext.getString(R.string.comm_call_phone));
        dialog_confirm.getCancelBtn().setText(mContext.getString(R.string.common_calcle));
        dialog_confirm.setConfirmClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_confirm.dismiss();
                startCallPhone(tel);
            }
        });
        dialog_confirm.setCancelClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog_confirm.dismiss();
            }
        });
        dialog_confirm.show();
    }
}
